home *** CD-ROM | disk | FTP | other *** search
- Path: ix.netcom.com!netnews
- From: miker3@ix.netcom.com (Mike Rubenstein)
- Newsgroups: comp.lang.c
- Subject: Re: is this string a number?
- Date: Tue, 16 Jan 1996 12:48:49 GMT
- Organization: Netcom
- Message-ID: <30fb9bf7.36131328@nntp.ix.netcom.com>
- References: <4dbogk$763@jupiter.planet.net> <30f9ac87.195826304@nntp.ix.netcom.com> <DL9tzD.H0@uns.bris.ac.uk>
- NNTP-Posting-Host: ix-dc6-24.ix.netcom.com
- X-NETCOM-Date: Tue Jan 16 4:48:41 AM PST 1996
- X-Newsreader: Forte Agent .99c/16.141
-
- nathan@pact.srf.ac.uk (Nathan Sidwell) wrote:
-
- |>Mike Rubenstein (miker3@ix.netcom.com) wrote:
- |>: Chris Kemp <chrisk@paladn.com> wrote:
- |>
- |>: |>I am inputting a string from the keyboard from a user, and
- |>: |>intend to use the atol or strtol functions to convert the
- |>: |>string to a number.
- |>
- |>: Use strtol() and supply a non-null second argument. When strtol()
- |>: returns, the pointer will point to the first character that was
- not
- |>: converted. Example:
- |>
- |>: #include <stdlib.h>
- |>
- |>: char *str;
- |>: char *nstr;
- |>: long l;
- |>
- |>: /* ... */
- |>
- |>: l = strtol(str, &nstr, 10);
- |>: if (nstr == str || *nstr != '\0')
- |>: {
- |>: /* str was empty or there is an illegal character */
- |>: }
- |>
- |>strtol et al remove leading white space, thus if the string " " is
- supplied
- |>the above check won't tell if the string was blank (nstr and str
- will
- |>be different and *nstr will be 0). Depending on
- |>your application this may or may not be a problem.
-
- Was it too much trouble to read the manual to find out what strtol()
- does?
-
- The problem you cite is not a problem because that's just not how
- strotol() works. From ISO 7.10.1.5:
-
- #include <stdlib.h>
- long int strtol(const char *nptr, char **endptr, int base);
-
- ...
-
- The subject sequence is defined as the longest initial
- subsequence of the input string, starting with the first
- non-white-space character character, that is of the expected
- form. The subject sequence contains no characters if the
- input string is empty or consists entirely of white space, or
- if the first non-white-space character is other than a sign
- or a permissible letter or digit.
-
- ...
-
- If the subject sequence is empty or does not have the expected
-
- form, no conversion is performed; the value of nptr is stored
- in the object ointed to by endptr, provided that endptr is not
-
- a null character.
-
-
- Michael M Rubenstein
-